e = sel => document.getElementById(sel)

/*
var log = function(s) {
    e("id-text-log").value += '\n' + s
}
*/
var log = console.log.bind(console)

var myUtils = {
    fps : 60,
    paused : false,
    clicked : false,
    bricks : [],
}

var collide = function(a, b){
    xa1 = a.x
    ya1 = a.y
    xa2 = a.x + a.img.width
    ya2 = a.y + a.img.height
    xb1 = b.x
    yb1 = b.y 
    xb2 = b.x + b.img.width
    yb2 = b.y + b.img.height

    if (Math.min(xa2,xb2) >= Math.max(xa1,xb1)
        && Math.min(ya2,yb2) >= Math.max(ya1,yb1)){
            return true
        }
    return false
}

var Levels = function(n){
    var bricks = []
    var x0 = 80
    var y0 = 100
    var lives = 1

    for (var i = 0; i < n; i++){
        x = x0 + (i%5) * myUtils.bricks.img.width
        y = y0 + Math.floor(i/5)*myUtils.bricks.img.height
        lives = Math.floor(Math.random()*10)
        bricks.push([x,y,lives])
    }

    return bricks
}


var LoadLevels = function(game, n){
    var bricks = []
    myUtils.bricks = Brick(game, [100,100])
    var ps = Levels(n)

    for (var i = 0; i < ps.length; i ++) {
        b = Brick(game, ps[i])
        bricks.push(b)
    }   
    return bricks
}

var DebugMode = function(game, enable){
    if (!enable){
        return
    }

    window.addEventListener("keydown", function(event){
        switch(event.code){
            case "Space": myUtils.paused = !myUtils.paused; break;
            case "Digit1": myUtils.bricks = LoadLevels(game, 1); break;
            case "Digit2": myUtils.bricks = LoadLevels(game, 2); break;
            case "Digit3": myUtils.bricks = LoadLevels(game, 3); break;
            case "Digit4": myUtils.bricks = LoadLevels(game, 4); break;
            case "Digit5": myUtils.bricks = LoadLevels(game, 5); break;
            case "Digit6": myUtils.bricks = LoadLevels(game, 6); break;
            case "Digit7": myUtils.bricks = LoadLevels(game, 7); break;
            case "Digit8": myUtils.bricks = LoadLevels(game, 8); break;
            case "Digit9": myUtils.bricks = LoadLevels(game, 9); break;
            case "Digit0": myUtils.bricks = LoadLevels(game, 10); break;
        }
    })

    document.getElementById("id-input-speed").addEventListener("input", function(event){
        myUtils.fps = event.target.value
    })
}